﻿using System;
using System.Drawing;
using System.IO;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SBPweb.PhotoGallery.Host
{
    public partial class GalleryEditor : System.Web.UI.UserControl
    {

        #region Private fields

        private string pViewerId;
        private Unit pWidth = 320;
        private Unit pHeight = 240;
        private Color pBackground = Color.DarkGray;
        private Color pForeground = Color.Gray;
        private Color pButtonBackground = Color.Black;
        private Color pButtonForeground = Color.White;
        private Color pButtonBorder = Color.White;
        private Color pButtonPlayForeground = Color.LightGreen;
        private Color pCaptionBackground = Color.Black;
        private Color pCaptionForeground = Color.White;
        private Color pContentBackground = Color.Black;
        private Color pSelectedThumbnailBorder = Color.Yellow;
        private Color pTrackerForeground = Color.Silver;
        private Font pTrackerFont = new Font(new FontFamily("Verdana"), 11, FontStyle.Regular);
        private Font pCaptionFont = new Font(new FontFamily("Verdana"), 11, FontStyle.Regular);
        private int pBorderRadius = 8;
        private int pPadding = 8;

        #endregion

        #region Property accessors

        public Slides Slides
        {
            get
            {
                if (Session[pViewerId + "_Slides"] != null)
                {
                    return Session[pViewerId + "_Slides"] as Slides;
                }
                return (Slides)(Session[pViewerId + "_Slides"] = new Slides());
            }
            set
            {
                Session[pViewerId + "_Slides"] = value;
            }
        }

        public string ViewerId
        {
            get
            {
                return pViewerId;
            }
            set
            {
                pViewerId = value;
            }
        }

        public Unit Width
        {
            get
            {
                return pWidth;
            }
            set
            {
                pWidth = value;
            }
        }

        public Unit Height
        {
            get
            {
                return pHeight;
            }
            set
            {
                pHeight = value;
            }
        }

        public Color Background
        {
            get
            {
                return pBackground;
            }
            set
            {
                pBackground = value;
            }
        }
       
        public Color Foreground
        {
            get
            {
                return pForeground;
            }
            set
            {
                pForeground = value;
            }
        }

        public Color ButtonBackground
        {
            get
            {
                return pButtonBackground;
            }
            set
            {
                pButtonBackground = value;
            }
        }

        public Color ButtonForeground
        {
            get
            {
                return pButtonForeground;
            }
            set
            {
                pButtonForeground = value;
            }
        }

        public Color ButtonBorder
        {
            get
            {
                return pButtonBorder;
            }
            set
            {
                pButtonBorder = value;
            }
        }

        public Color CaptionBackground
        {
            get
            {
                return pCaptionBackground;
            }
            set
            {
                pCaptionBackground = value;
            }
        }

        public Color CaptionForeground
        {
            get
            {
                return pCaptionForeground;
            }
            set
            {
                pCaptionForeground = value;
            }
        }

        public Color ContentBackground
        {
            get
            {
                return pContentBackground;
            }
            set
            {
                pContentBackground = value;
            }
        }

        public Color TrackerForeground
        {
            get
            {
                return pTrackerForeground;
            }
            set
            {
                pTrackerForeground = value;
            }
        }

        public Font CaptionFont
        {
            get
            {
                return pCaptionFont;
            }
            set
            {
                pCaptionFont = value;
            }
        }

        public Font TrackerFont
        {
            get
            {
                return pTrackerFont;
            }
            set
            {
                pTrackerFont = value;
            }
        }

        public int BorderRadius
        {
            get
            {
                return pBorderRadius;
            }
            set
            {
                pBorderRadius = value;
            }
        }

        public int Padding
        {
            get
            {
                return pPadding;
            }
            set
            {
                pPadding = value;
            }
        }
        
        #endregion

        #region Methods

        public void AddFile()
        {
            if (!fuPhoto.HasFile || fuPhoto.PostedFile.ContentLength == 0)
            {
                return;
            }
            Slide slide;
            try
            {
                Uri postedUri = new Uri(fuPhoto.PostedFile.FileName, UriKind.RelativeOrAbsolute);
                string fileName;
                if (postedUri.IsAbsoluteUri)
                {
                    fileName = postedUri.Segments[postedUri.Segments.Length - 1];
                }
                else
                {
                    fileName = postedUri.OriginalString; //Uri.EscapeUriString(postedUri.OriginalString);
                }
                string thumbFileName = fileName;
                Uri uri = new Uri(string.Format("{0}{1}{2}", HostingEnvironment.ApplicationHost.GetPhysicalPath(), Slides.BasePath, fileName), UriKind.RelativeOrAbsolute);
                fuPhoto.PostedFile.SaveAs(uri.LocalPath);

//                slide = new Slide(fileName, thumbFileName, string.Format("{0}, {1}", uri.LocalPath, fileName));
                slide = new Slide(fileName, thumbFileName, tbComment.Text);
                Slides.Add(slide);

                tbComment.Text = string.Empty;
            }
            catch
            {
            }
        }

        public string GetColorName(Color color)
        {
            if (pBackground.IsKnownColor)
            {
                string[] split = color.ToString().Split(new char[] { '[', ']' });

                return split[1];
            }
            return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
        }

        public string GetBoolean(bool boolean)
        {
            return boolean.ToString().ToLower();
        }

        public string GetFontFamily(Font font)
        {
            return font.FontFamily.Name;
        }

        public string GetFontSize(Font font)
        {
            return font.Size.ToString();
        }

        public string GetFontStyle(Font font)
        {
            switch (font.Style)
            {
                default:
                case FontStyle.Regular:
                    return "Normal";
                case FontStyle.Italic:
                    return "Italic";
            }
        }

        public string GetFontWeight(Font font)
        {
            if (font.Bold)
            {
                return "Bold";
            }
            return "Normal";
        }

        private void SetButtonStyleProperties(WebControl ctrl)
        {
            ctrl.Style.Add(HtmlTextWriterStyle.BorderStyle, "solid");
            ctrl.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
            ctrl.Style.Add(HtmlTextWriterStyle.BackgroundColor, GetColorName(ButtonBackground));
            ctrl.Style.Add(HtmlTextWriterStyle.BorderColor, GetColorName(ButtonBorder));
            ctrl.Style.Add(HtmlTextWriterStyle.Color, GetColorName(ButtonForeground));
            ctrl.Style.Add("opacity", "0.5");
            ctrl.Attributes.Add("onmouseover", "javascript: this.style.opacity = 1.0;");
            ctrl.Attributes.Add("onmouseout", "javascript: this.style.opacity = 0.5;");
        }

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            SetButtonStyleProperties(bUpload);
            SetButtonStyleProperties(tbFile);
            SetButtonStyleProperties(tbComment);
        }

        protected void bUpload_Click(object sender, EventArgs e)
        {
            AddFile();
        }

        #endregion

        #endregion

    }
}